<?php

/**
 * This is the model class for table "{{topic}}".
 *
 * The followings are the available columns in table '{{topic}}':
 * @property integer $id
 * @property string $keywords
 * @property integer $total_view
 * @property integer $total_like
 * @property integer $total_comment
 * @property integer $status_code
 * @property string $create_timezone
 * @property string $create_timestamp
 * @property string $create_datetime
 * @property string $create_date
 * @property string $create_time
 * @property integer $create_by
 * @property string $update_timezone
 * @property string $update_timestamp
 * @property string $update_datetime
 * @property string $update_date
 * @property string $update_time
 * @property integer $update_by
 */
class Topic extends ActiveRecord
{
	
	public $status_;
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{topic}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			// compulsory
			array('var_', 'ext.validators.BotDetectionValidator'),
			array('var_', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'creator' => array(self::BELONGS_TO, 'User', 'create_by'),
			'editor' => array(self::BELONGS_TO, 'User', 'update_by'),
			'html' => array(self::HAS_ONE, 'Content', 'module_id', 'scopes' => array('module' => __CLASS__, 'language' => $this->defaultLanguage)),
			'defaultHtml' => array(self::HAS_ONE, 'Content', 'module_id', 'scopes' => array('module' => __CLASS__, 'language' => $this->defaultLanguage)),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'keywords' => 'Keywords',
			'total_view' => 'Total View',
			'total_like' => 'Total Like',
			'total_comment' => 'Total Comment',
			'status_code' => 'Status Code',
			'create_timezone' => 'Create Time Zone',
			'create_timestamp' => 'Create Timestamp',
			'create_datetime' => 'Create Datetime',
			'create_date' => 'Create Date',
			'create_time' => 'Create Time',
			'create_by' => 'Create By',
			'update_timezone' => 'Update Time Zone',
			'update_timestamp' => 'Update Timestamp',
			'update_datetime' => 'Update Datetime',
			'update_date' => 'Update Date',
			'update_time' => 'Update Time',
			'update_by' => 'Update By',
		);
	}

	public function scopes() {
		$t=$this->getTableAlias(false);
		return array(
				'published' => array(
						'condition' => $t.'.status_code = :activeStatus'.$t,
						'params' => array(
								':activeStatus'.$t => Option::STATUS_ACTIVE,
						),
				),
				'accessible'=>array(
						'condition' => $t.'.status_code <> :deletedStatus'.$t,
						'params' => array(
								':deletedStatus'.$t=>Option::STATUS_DELETED,
						),
				),
		);
	}
	
	public function autoFill(){
		switch($this->scenario){
			case 'insert':
			case 'adminCreate':
				$this->status_ = Option::STATUS_ACTIVE;
				break;
	
			case 'update':
			case 'adminUpdate':
				$this->status_ = $this->status_code;
				break;
		}
	}
	
	protected function beforeSave(){
		if(parent::beforeSave()){
	
			$this->status_code = $this->status_;
	
			if ($this->isNewRecord) {
				$this->setCreateDateTime();
			} else {
				$this->setUpdateDateTime();
			}
			return true;
		}else
			return false;
	}
	

	public function updatePostCounter($increment=1){
		return $this->updateCounters(array('total_post'=>$increment), 'id=:id', array(':id'=>$this->id));
	}	
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function userSearch()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$t = $this->getTableAlias(false);
		$criteria=new CDbCriteria;

		$criteria->compare($t.'.id',$this->id_);
		$criteria->compare($t.'.keywords',$this->keywords_,true);
		$criteria->compare($t.'.status_code',$this->status_);

		return new ActiveDataProvider($this->published(), array(
			'criteria'=>$criteria,
		));
	}
	
	public function adminSearch(){
		
		$t = $this->getTableAlias(false);
		
		$criteria=new CDbCriteria;
		
		$criteria->compare($t.'.id',$this->id_);
		$criteria->compare($t.'.keywords',$this->keywords_,true);
		$criteria->compare($t.'.status_code',$this->status_);
		
		return new ActiveDataProvider($this->accessible(), array(
				'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Topic the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getTotalPost(){
		return $this->total_post;
	}
	
	public function getUrl(){
		return Yii::app()->createAbsoluteUrl('topic/view', array(
			'id'=>$this->id,
			'title'=>$this->urlFormat($this->title),
		));
	}
	
	public function getDeleteUrl(){
		return Yii::app()->createAbsoluteUrl('topic/delete', array(
			'id'=>$this->id,
			'title'=>$this->urlFormat($this->title),
		));
	}
	
}
